<template>
  <div class="changdi-container">
    <Header />
    <div class="management-main">
      <div class="left-nav">
        <button class="nav-item active" @click="activeTab = 'reservation'">场地预约</button>
        <button class="nav-item" @click="activeTab = 'history'">预约历史</button>
      </div>
      
      <div class="content-area">
        <!-- 预约表单 -->
        <div v-if="activeTab === 'reservation'" class="module-card">
          <div class="module-header">
            <h3>场地预约</h3>
            <p class="subtitle">请选择需要预约的场地和时间段</p>
          </div>
          
          <form class="reservation-form" @submit.prevent="submitReservation">
            <div class="form-group">
              <label>选择场地 <span class="required">*</span></label>
              <select v-model="formData.location" required>
                <option value="">-- 请选择 --</option>
                <option value="meeting">会议室</option>
                <option value="broadcast">导播间</option>
              </select>
            </div>
            
            <div class="form-group">
              <label>选择日期 <span class="required">*</span></label>
              <input type="date" v-model="formData.date" :min="minDate" required>
            </div>
            
            <div class="form-group">
              <label>选择时间段 <span class="required">*</span></label>
              <div class="time-slots">
                <div 
                  class="time-slot" 
                  v-for="(slot, index) in timeSlots" 
                  :key="index"
                  :class="{ selected: formData.timeSlot === slot.value, disabled: isSlotDisabled(slot.value) }"
                  @click="formData.timeSlot = !isSlotDisabled(slot.value) ? slot.value : formData.timeSlot"
                >
                  {{ slot.label }}
                </div>
              </div>
            </div>
            
            <div class="form-group">
              <label>申请人 <span class="required">*</span></label>
              <input type="text" v-model="formData.applicant" placeholder="请输入姓名" required>
            </div>
            
            <div class="form-group">
              <label>用途 <span class="required">*</span></label>
              <textarea v-model="formData.purpose" placeholder="请简要说明使用用途" rows="3" required></textarea>
            </div>
            
            <div class="form-actions">
              <button type="submit" class="btn-primary">提交预约</button>
              <button type="reset" class="btn-secondary" @click="resetForm">重置</button>
            </div>
          </form>
        </div>
        
        <!-- 预约历史 -->
        <div v-if="activeTab === 'history'" class="module-card">
          <div class="module-header">
            <h3>预约历史</h3>
          </div>
          
          <div class="history-list">
            <div class="history-item header">
              <div class="col">场地</div>
              <div class="col">日期</div>
              <div class="col">时间段</div>
              <div class="col">申请人</div>
              <div class="col">用途</div>
              <div class="col">状态</div>
            </div>
            
            <div class="history-item" v-for="(item, index) in reservationHistory" :key="index">
              <div class="col">{{ item.location === 'meeting' ? '会议室' : '导播间' }}</div>
              <div class="col">{{ formatDate(item.date) }}</div>
              <div class="col">{{ getTimeSlotLabel(item.timeSlot) }}</div>
              <div class="col">{{ item.applicant }}</div>
              <div class="col">{{ item.purpose }}</div>
              <div class="col">
                <span :class="`status ${item.status}`">{{ 
                  item.status === 'pending' ? '待审核' : 
                  item.status === 'approved' ? '已通过' : '已拒绝' 
                }}</span>
              </div>
            </div>
            
            <div class="empty-state" v-if="reservationHistory.length === 0">
              <i class="iconfont icon-empty"></i>
              <p>暂无预约记录</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <Footer />
  </div>
</template>

<script setup>
import Header from '@/components/Header.vue'
import Footer from '@/components/Footer.vue'
import { ref, computed } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useRouter } from 'vue-router'

const authStore = useAuthStore()
const router = useRouter()

// 检查登录状态
if (!authStore.isLoggedIn) {
  router.push('/login')
}

// 状态管理
const activeTab = ref('reservation')

// 时间槽配置
const timeSlots = [
  { label: '8:00-9:40', value: 'morning1' },
  { label: '10:00-11:40', value: 'morning2' },
  { label: '13:20-15:00', value: 'afternoon1' },
  { label: '15:20-17:00', value: 'afternoon2' },
  { label: '18:00-20:30', value: 'evening' }
]

// 表单数据
const formData = ref({
  location: '',
  date: '',
  timeSlot: '',
  applicant: '',
  purpose: ''
})

// 最小日期（今天）
const minDate = computed(() => {
  const today = new Date()
  return today.toISOString().split('T')[0]
})

// 模拟已预约数据
const reservedSlots = ref([
  { location: 'meeting', date: '2024-06-15', timeSlot: 'morning1' },
  { location: 'broadcast', date: '2024-06-15', timeSlot: 'afternoon1' }
])

// 预约历史
const reservationHistory = ref([
  {
    location: 'meeting',
    date: '2024-06-10',
    timeSlot: 'afternoon1',
    applicant: '张三',
    purpose: '部门例会',
    status: 'approved'
  },
  {
    location: 'broadcast',
    date: '2024-06-12',
    timeSlot: 'evening',
    applicant: '李四',
    purpose: '节目录制',
    status: 'pending'
  }
])

// 检查时间段是否已被预约
const isSlotDisabled = (timeSlot) => {
  if (!formData.value.location || !formData.value.date) return false
  return reservedSlots.value.some(slot => 
    slot.location === formData.value.location && 
    slot.date === formData.value.date && 
    slot.timeSlot === timeSlot
  )
}

// 提交预约
const submitReservation = () => {
  if (!formData.value.location || !formData.value.date || !formData.value.timeSlot) {
    alert('请填写完整预约信息')
    return
  }
  
  if (isSlotDisabled(formData.value.timeSlot)) {
    alert('该时间段已被预约，请选择其他时间段')
    return
  }
  
  // 添加到已预约列表
  reservedSlots.value.push({...formData.value})
  
  // 添加到历史记录
  reservationHistory.value.unshift({
    ...formData.value,
    status: 'pending'
  })
  
  // 重置表单
  resetForm()
  
  alert('预约申请已提交，请等待审核')
}

// 重置表单
const resetForm = () => {
  formData.value = {
    location: '',
    date: '',
    timeSlot: '',
    applicant: '',
    purpose: ''
  }
}

// 格式化日期显示
const formatDate = (dateStr) => {
  const options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' }
  return new Date(dateStr).toLocaleDateString('zh-CN', options)
}

// 获取时间段标签
const getTimeSlotLabel = (value) => {
  const slot = timeSlots.find(s => s.value === value)
  return slot ? slot.label : ''
}
</script>

<style scoped>
.changdi-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.management-main {
  flex: 1;
  display: flex;
  gap: 20px;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

.left-nav {
  width: 200px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.nav-item {
  padding: 12px 15px;
  background-color: white;
  border: none;
  border-radius: 8px;
  text-align: left;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 1rem;
  font-weight: 500;
  color: #333;
  box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}

.nav-item:hover {
  background-color: #f0f7ff;
  color: #3498db;
}

.nav-item.active {
  background-color: #3498db;
  color: white;
}

.content-area {
  flex: 1;
}

.module-card {
  background-color: white;
  border-radius: 10px;
  padding: 25px;
  box-shadow: 0 3px 10px rgba(0,0,0,0.08);
  margin-bottom: 20px;
}

.module-header {
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.module-header h3 {
  font-size: 1.5rem;
  color: #2c3e50;
  margin-bottom: 5px;
}

.subtitle {
  color: #7f8c8d;
  font-size: 0.9rem;
}

.reservation-form {
  display: grid;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-weight: 500;
  color: #34495e;
}

.required {
  color: #e74c3c;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 12px 15px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.1);
}

.time-slots {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  margin-top: 5px;
}

.time-slot {
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
}

.time-slot:hover {
  border-color: #3498db;
  background-color: #f0f7ff;
}

.time-slot.selected {
  background-color: #3498db;
  color: white;
  border-color: #3498db;
}

.time-slot.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background-color: #f9f9f9;
}

.form-actions {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.btn-primary {
  padding: 12px 25px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.3s;
  font-weight: 500;
}

.btn-primary:hover {
  background-color: #2980b9;
}

.btn-secondary {
  padding: 12px 25px;
  background-color: #ecf0f1;
  color: #34495e;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.3s;
  font-weight: 500;
}

.btn-secondary:hover {
  background-color: #dcdde1;
}

/* 历史记录样式 */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-item {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 2fr 1fr;
  gap: 10px;
  padding: 15px;
  border-radius: 8px;
  align-items: center;
}

.history-item.header {
  background-color: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.history-item:not(.header) {
  background-color: #fcfcfc;
  border: 1px solid #f1f1f1;
}

.col {
  padding: 0 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.8rem;
}

.status.pending {
  background-color: #fff3cd;
  color: #856404;
}

.status.approved {
  background-color: #d4edda;
  color: #155724;
}

.status.rejected {
  background-color: #f8d7da;
  color: #721c24;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #7f8c8d;
}

.empty-state i {
  font-size: 3rem;
  margin-bottom: 15px;
  color: #bdc3c7;
}

@media (max-width: 900px) {
  .management-main {
    flex-direction: column;
  }
  
  .left-nav {
    width: 100%;
    flex-direction: row;
    overflow-x: auto;
    padding-bottom: 10px;
  }
  
  .history-item {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  
  .history-item.header .col:nth-child(n+4),
  .history-item .col:nth-child(n+4) {
    display: none;
  }
}
</style>